#include"test.h"

typedef int datatype;

typedef struct ListNode
{
	datatype data;
	struct ListNode* next;
}ListNode;

void Print(ListNode* phead)
{
	ListNode* ptail = phead;
	while (ptail)
	{
		printf("%d ", ptail->data);
		ptail = ptail->next;
	}
	printf("\n");
}

ListNode* CreatNode(datatype x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void InsertSL(ListNode** node, datatype x)
{
	ListNode* newnode = CreatNode(x);

	if (*node == NULL)
	{
		*node = newnode;
	}
	else
	{
		ListNode* ptail = *node;
		while (ptail->next != NULL)
		{
			ptail = ptail->next;
		}
		ptail->next = newnode;
	}

}



void homework(ListNode** complete, ListNode* node1, ListNode* node2)
{
	int i = 1;
	if (*complete == NULL)
	{
		ListNode* newnode = CreatNode(node1->data);
		*complete = newnode;
		node1 = node1->next;
		i++;
	}
	while (node1 && node2)
	{
		if (i % 2 != 0)
		{
			ListNode* newnode = CreatNode(node1->data);
			ListNode* ptail = *complete;
			while (ptail->next)
			{
				ptail = ptail->next;
			}
			ptail->next = newnode;
			node1 = node1->next;
		}
		else
		{
			ListNode* newnode = CreatNode(node2->data);
			ListNode* ptail = *complete;
			while (ptail->next)
			{
				ptail = ptail->next;
			}
			ptail->next = newnode;
			node2 = node2->next;
		}
		i++;
	}

	if (node1 == NULL)
	{
		while (node2)
		{
			ListNode* newnode = CreatNode(node2->data);
			ListNode* ptail = *complete;
			while (ptail->next)
			{
				ptail = ptail->next;
			}
			ptail->next = newnode;
			node2 = node2->next;
		}
	}

	if (node2 == NULL)
	{
		while (node1)
		{
			ListNode* newnode = CreatNode(node1->data);
			ListNode* ptail = *complete;
			while (ptail->next)
			{
				ptail = ptail->next;
			}
			ptail->next = newnode;
			node1 = node1->next;
		}
	}
}

int main()
{	
	ListNode* node1 = NULL;
	ListNode* node2 = NULL;
	ListNode* complete = NULL;

	InsertSL(&node1, 1);
	InsertSL(&node1, 3);
	InsertSL(&node1, 5);
	//Print(node1);

	InsertSL(&node2, 2);
	InsertSL(&node2, 4);
	InsertSL(&node2, 6);
	//Print(node2);
	
	homework(&complete, node1, node2);
	Print(complete);

	return 0;
}